package com.lovo.homework7_13DouDiZhu;

import com.lovo.homework7_11DouDiZhu.Card;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class DouDiZhu extends JFrame {
    public static ArrayList<Card> list = new ArrayList<Card>();
    public DouDiZhu(){
        //2.设置空布局，要写在设置窗口的上面
        this.setLayout(null);

        //3.tianjia
        String str ="";
        int y = 20;
        for (int i = 3; i <=15; i++) {
            for (int j = 1; j <=4; j++) {
                Card card =new Card(i,j);
                list.add(card);
            }
        }
        Card cardBig = new Card(17,1);
        Card cardLittle = new Card(16,1);
        list.add(cardBig);
        list.add(cardLittle);

        Collections.shuffle(list);

        ArrayList<Card> a = new ArrayList<>();
        ArrayList<Card> b = new ArrayList<>();
        ArrayList<Card> c = new ArrayList<>();
        for (int i = 0; i < (list.size() - 3); i++) {
            Card newCard = list.get(i);
            if (i % 3 == 0) {
                a.add(newCard);
            } else if (i % 3 == 1) {
                b.add(newCard);
            } else {
                c.add(newCard);
            }
        }

        a.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPoint()==o2.getPoint()){
                    return o1.getType()-o2.getType();
                }else {
                    return o1.getPoint()-o2.getPoint();
                }
            }
        });
        b.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPoint()==o2.getPoint()){
                    return o1.getType()-o2.getType();
                }else {
                    return o1.getPoint()-o2.getPoint();
                }
            }
        });
        c.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPoint()==o2.getPoint()){
                    return o1.getType()-o2.getType();
                }else {
                    return o1.getPoint()-o2.getPoint();
                }
            }
        });
        int height = 20;
        for (int i = (a.size()-1); i >=0; i--) {//17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
            str+="/images/"+ a.get(i).getPoint()+"_"+ a.get(i).getType()+".jpg";
            height+=30;
            Icon lableImage =util.DouDiZhu.getIcon(str);
            JLabel labelImg = new JLabel(lableImage);
            labelImg.setBounds(20,height,105,150);
            this.add(labelImg);
            str="";
        }
        int height1 = 20;
        for (int i = (b.size()-1); i >=0; i--) {//17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
            str+="/images/"+ b.get(i).getPoint()+"_"+ b.get(i).getType()+".jpg";
            height1+=30;
            Icon lableImage =util.DouDiZhu.getIcon(str);
            JLabel labelImg = new JLabel(lableImage);
            labelImg.setBounds(1300,height1,105,150);
            this.add(labelImg);
            str="";
        }
        int height2 = 400;
        for (int i = (a.size()-1); i >=0; i--) {//17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
            str+="/images/"+ c.get(i).getPoint()+"_"+ c.get(i).getType()+".jpg";
            height2+=30;
            Icon lableImage =util.DouDiZhu.getIcon(str);
            JLabel labelImg = new JLabel(lableImage);
            labelImg.setBounds(height2,600,105,150);
            this.add(labelImg);
            str="";
        }


        //1.设置窗口
        //设置窗口大小
        this.setSize(1500,800);
        //设置窗口的标题
        this.setTitle("LOL");
        //设置打开窗口的初始位置
        this.setLocationRelativeTo(null);//这句话一定要放在设置窗口大小的下面 自己理解原因
        //设置窗口关闭时，程序结束
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置窗口可视化
        this.setVisible(true);//这句话写在构造方法的最后一句。此时点击关闭窗口，窗口会消失，只是看不见而已，仍然会占用内存，因此要写 setDefaultCloseOperation
    }

    public static void main(String[] args) {
        new DouDiZhu();
    }
}
